home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / (gcc-1.37.π) / genemit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-02  |  11.5 KB  |  481 lines  |  [TEXT/KAHL]

  1. /* Generate code from machine description to emit insns as rtl.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "obstack.h"
  25.  
  26. struct obstack obstack;
  27. struct obstack *rtl_obstack = &obstack;
  28.  
  29. #define obstack_chunk_alloc xmalloc
  30. #define obstack_chunk_free free
  31. extern int xmalloc ();
  32. extern void free ();
  33.  
  34. void fatal ();
  35. void fancy_abort ();
  36.  
  37. int max_opno;
  38. int max_dup_opno;
  39. int register_constraints;
  40. int insn_code_number;
  41.  
  42. #define max(a, b) ((a) > (b) ? (a) : (b))
  43.  
  44. void
  45. max_operand_1 (x)
  46.      rtx x;
  47. {
  48.   register RTX_CODE code;
  49.   register int i;
  50.   register int len;
  51.   register char *fmt;
  52.  
  53.   if (x == 0)
  54.     return;
  55.  
  56.   code = GET_CODE (x);
  57.  
  58.   if (code == MATCH_OPERAND && XSTR (x, 2) != 0)
  59.     register_constraints = 1;
  60.   if (code == MATCH_OPERAND || code == MATCH_OPERATOR)
  61.     max_opno = max (max_opno, XINT (x, 0));
  62.   if (code == MATCH_DUP)
  63.     max_dup_opno = max (max_dup_opno, XINT (x, 0));
  64.  
  65.   fmt = GET_RTX_FORMAT (code);
  66.   len = GET_RTX_LENGTH (code);
  67.   for (i = 0; i < len; i++)
  68.     {
  69.       if (fmt[i] == 'e' || fmt[i] == 'u')
  70.     max_operand_1 (XEXP (x, i));
  71.       else if (fmt[i] == 'E')
  72.     {
  73.       int j;
  74.       for (j = 0; j < XVECLEN (x, i); j++)
  75.         max_operand_1 (XVECEXP (x, i, j));
  76.     }
  77.     }
  78. }
  79.  
  80. int
  81. max_operand_vec (insn, arg)
  82.      rtx insn;
  83.      int arg;
  84. {
  85.   register int len = XVECLEN (insn, arg);
  86.   register int i;
  87.  
  88.   max_opno = -1;
  89.   max_dup_opno = -1;
  90.  
  91.   for (i = 0; i < len; i++)
  92.     max_operand_1 (XVECEXP (insn, arg, i));
  93.  
  94.   return max_opno + 1;
  95. }
  96.  
  97. void
  98. print_code (code)
  99.      RTX_CODE code;
  100. {
  101.   register char *p1;
  102.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  103.     {
  104.       if (*p1 >= 'a' && *p1 <= 'z')
  105.     putchar (*p1 + 'A' - 'a');
  106.       else
  107.     putchar (*p1);
  108.     }
  109. }
  110.  
  111. /* Print a C expression to construct an RTX just like X,
  112.    substituting any operand references appearing within.  */
  113.  
  114. void
  115. gen_exp (x)
  116.      rtx x;
  117. {
  118.   register RTX_CODE code;
  119.   register int i;
  120.   register int len;
  121.   register char *fmt;
  122.  
  123.   if (x == 0)
  124.     {
  125.       printf ("0");
  126.       return;
  127.     }
  128.  
  129.   code = GET_CODE (x);
  130.  
  131.   switch (code)
  132.     {
  133.     case MATCH_OPERAND:
  134.     case MATCH_DUP:
  135.       printf ("operand%d", XINT (x, 0));
  136.       return;
  137.  
  138.     case MATCH_OPERATOR:
  139.       printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
  140.       printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  141.       for (i = 0; i < XVECLEN (x, 2); i++)
  142.     {
  143.       printf (",\n\t\t");
  144.       gen_exp (XVECEXP (x, 2, i));
  145.     }
  146.       printf (")");
  147.       return;
  148.  
  149.     case ADDRESS:
  150.       fatal ("ADDRESS expression code used in named instruction pattern");
  151.  
  152.     case PC:
  153.       printf ("pc_rtx");
  154.       return;
  155.  
  156.     case CC0:
  157.       printf ("cc0_rtx");
  158.       return;
  159.  
  160.     case CONST_INT:
  161.       if (INTVAL (x) == 0)
  162.     {
  163.       printf ("const0_rtx");
  164.       return;
  165.     }
  166.       if (INTVAL (x) == 1)
  167.     {
  168.       printf ("const1_rtx");
  169.       return;
  170.     }
  171.     }
  172.  
  173.   printf ("gen_rtx (");
  174.   print_code (code);
  175.   printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
  176.  
  177.   fmt = GET_RTX_FORMAT (code);
  178.   len = GET_RTX_LENGTH (code);
  179.   for (i = 0; i < len; i++)
  180.     {
  181.       if (fmt[i] == '0')
  182.     break;
  183.       printf (", ");
  184.       if (fmt[i] == 'e' || fmt[i] == 'u')
  185.     gen_exp (XEXP (x, i));
  186.       else if (fmt[i] == 'i')
  187.     printf ("%d", XINT (x, i));
  188.       else if (fmt[i] == 's')
  189.     printf ("\"%s\"", XSTR (x, i));
  190.       else if (fmt[i] == 'E')
  191.     {
  192.       int j;
  193.       printf ("gen_rtvec (%d", XVECLEN (x, i));
  194.       for (j = 0; j < XVECLEN (x, i); j++)
  195.         {
  196.           printf (",\n\t\t");
  197.           gen_exp (XVECEXP (x, i, j));
  198.         }
  199.       printf (")");
  200.     }
  201.       else
  202.     abort ();
  203.     }
  204.   printf (")");
  205. }  
  206.  
  207. /* Generate the `gen_...' function for a DEFINE_INSN.  */
  208.  
  209. void
  210. gen_insn (insn)
  211.      rtx insn;
  212. {
  213.   int operands;
  214.   register int i;
  215.  
  216.   /* Don't mention instructions whose names are the null string.
  217.      They are in the machine description just to be recognized.  */
  218.   if (strlen (XSTR (insn, 0)) == 0)
  219.     return;
  220.  
  221.   /* Find out how many operands this function has,
  222.      and also whether any of them have register constraints.  */
  223.   register_constraints = 0;
  224.   operands = max_operand_vec (insn, 1);
  225.   if (max_dup_opno >= operands)
  226.     fatal ("match_dup operand number has no match_operand");
  227.  
  228.   /* Output the function name and argument declarations.  */
  229.   printf ("rtx\ngen_%s (", XSTR (insn, 0));
  230.   for (i = 0; i < operands; i++)
  231.     printf (i ? ", operand%d" : "operand%d", i);
  232.   printf (")\n");
  233.   for (i = 0; i < operands; i++)
  234.     printf ("     rtx operand%d;\n", i);
  235.   printf ("{\n");
  236.  
  237.   /* Output code to construct and return the rtl for the instruction body */
  238.  
  239.   if (XVECLEN (insn, 1) == 1)
  240.     {
  241.       printf ("  return ");
  242.       gen_exp (XVECEXP (insn, 1, 0));
  243.       printf (";\n}\n\n");
  244.     }
  245.   else
  246.     {
  247.       printf ("  return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
  248.       for (i = 0; i < XVECLEN (insn, 1); i++)
  249.     {
  250.       printf (",\n\t\t");
  251.       gen_exp (XVECEXP (insn, 1, i));
  252.     }
  253.       printf ("));\n}\n\n");
  254.     }
  255. }
  256.  
  257. /* Generate the `gen_...' function for a DEFINE_EXPAND.  */
  258.  
  259. void
  260. gen_expand (expand)
  261.      rtx expand;
  262. {
  263.   int operands;
  264.   register int i;
  265.  
  266.   if (strlen (XSTR (expand, 0)) == 0)
  267.     fatal ("define_expand lacks a name");
  268.   if (XVEC (expand, 1) == 0)
  269.     fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
  270.  
  271.   /* Find out how many operands this function has,
  272.      and also whether any of them have register constraints.  */
  273.   register_constraints = 0;
  274.  
  275.   operands = max_operand_vec (expand, 1);
  276.  
  277.   /* Output the function name and argument declarations.  */
  278.   printf ("rtx\ngen_%s (", XSTR (expand, 0));
  279.   for (i = 0; i < operands; i++)
  280.     printf (i ? ", operand%d" : "operand%d", i);
  281.   printf (")\n");
  282.   for (i = 0; i < operands; i++)
  283.     printf ("     rtx operand%d;\n", i);
  284.   printf ("{\n");
  285.  
  286.   /* For each operand referred to only with MATCH_DUPs,
  287.      make a local variable.  */
  288.   for (i = operands; i <= max_dup_opno; i++)
  289.     printf ("  rtx operand%d;\n", i);
  290.   printf ("  rtx operands[%d];\n", max (operands, max_dup_opno + 1));
  291.   printf ("  rtx _val;\n");
  292.   printf ("  start_sequence ();\n");
  293.  
  294.   /* The fourth operand of DEFINE_EXPAND is some code to be executed
  295.      before the actual construction.
  296.      This code expects to refer to `operands'
  297.      just as the output-code in a DEFINE_INSN does,
  298.      but here `operands' is an automatic array.
  299.      So copy the operand values there before executing it.  */
  300.   if (XSTR (expand, 3))
  301.     {
  302.       /* Output code to copy the arguments into `operands'.  */
  303.       for (i = 0; i < operands; i++)
  304.     printf ("  operands[%d] = operand%d;\n", i, i);
  305.  
  306.       /* Output the special code to be executed before the sequence
  307.      is generated.  */
  308.       printf ("%s\n", XSTR (expand, 3));
  309.  
  310.       /* Output code to copy the arguments back out of `operands'
  311.      (unless we aren't going to use them at all).  */
  312.       if (XVEC (expand, 1) != 0)
  313.     {
  314.       for (i = 0; i < operands; i++)
  315.         printf ("  operand%d = operands[%d];\n", i, i);
  316.       for (; i <= max_dup_opno; i++)
  317.         printf ("  operand%d = operands[%d];\n", i, i);
  318.     }
  319.     }
  320.  
  321.   /* Output code to construct the rtl for the instruction bodies.
  322.      Use emit_insn to add them to the sequence being accumulated.
  323.      But don't do this if the user's code has set `no_more' nonzero.  */
  324.  
  325.   for (i = 0; i < XVECLEN (expand, 1); i++)
  326.     {
  327.       rtx next = XVECEXP (expand, 1, i);
  328.       if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
  329.       || (GET_CODE (next) == PARALLEL
  330.           && GET_CODE (XVECEXP (next, 0, 0)) == SET
  331.           && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
  332.       || GET_CODE (next) == RETURN)
  333.     printf ("  emit_jump_insn (");
  334.       else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
  335.            || GET_CODE (next) == CALL
  336.            || (GET_CODE (next) == PARALLEL
  337.            && GET_CODE (XVECEXP (next, 0, 0)) == SET
  338.            && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
  339.            || (GET_CODE (next) == PARALLEL
  340.            && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
  341.     printf ("  emit_call_insn (");
  342.       else if (GET_CODE (next) == CODE_LABEL)
  343.     printf ("  emit_label (");
  344.       else if (GET_CODE (next) == MATCH_OPERAND
  345.            || GET_CODE (next) == MATCH_OPERATOR
  346.            || GET_CODE (next) == MATCH_DUP
  347.            || GET_CODE (next) == PARALLEL)
  348.     printf ("  emit (");
  349.       else
  350.     printf ("  emit_insn (");
  351.       gen_exp (next);
  352.       printf (");\n");
  353.       if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
  354.       && GET_CODE (SET_SRC (next)) == LABEL_REF)
  355.     printf ("  emit_barrier ();");
  356.     }
  357.  
  358.   /* Call `gen_sequence' to make a SEQUENCE out of all the
  359.      insns emitted within this gen_... function.  */
  360.  
  361.   printf (" _done:\n");
  362.   printf ("  _val = gen_sequence ();\n");
  363.   printf ("  end_sequence ();\n");
  364.   printf ("  return _val;\n}\n\n");
  365. }
  366.  
  367. int
  368. xmalloc (size)
  369. {
  370.   register int val = (int)malloc (size);
  371.  
  372.   if (val == 0)
  373.     fatal ("virtual memory exhausted");
  374.  
  375.   return val;
  376. }
  377.  
  378. int
  379. xrealloc (ptr, size)
  380.      char *ptr;
  381.      int size;
  382. {
  383.   int result = (int)realloc (ptr, size);
  384.   if (!result)
  385.     fatal ("virtual memory exhausted");
  386.   return result;
  387. }
  388.  
  389. void
  390. fatal (s, a1, a2)
  391.      char *s;
  392. {
  393.   fprintf (stderr, "genemit: ");
  394.   fprintf (stderr, s, a1, a2);
  395.   fprintf (stderr, "\n");
  396.   exit (FATAL_EXIT_CODE);
  397. }
  398.  
  399. /* More 'friendly' abort that prints the line and file.
  400.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  401.  
  402. void
  403. fancy_abort ()
  404. {
  405.   fatal ("Internal gcc abort.");
  406. }
  407.  
  408. int
  409. main (argc, argv)
  410.      int argc;
  411.      char **argv;
  412. {
  413.   rtx desc;
  414.   FILE *infile;
  415.   extern rtx read_rtx ();
  416.   register int c;
  417.  
  418.   obstack_init (rtl_obstack);
  419.  
  420.   if (argc <= 1)
  421.     fatal ("No input file name.");
  422.  
  423.   infile = fopen (argv[1], "r");
  424.   if (infile == 0)
  425.     {
  426.       perror (argv[1]);
  427.       exit (FATAL_EXIT_CODE);
  428.     }
  429.  
  430.   init_rtl ();
  431.  
  432.   /* Assign sequential codes to all entries in the machine description
  433.      in parallel with the tables in insn-output.c.  */
  434.  
  435.   insn_code_number = 0;
  436.  
  437.   printf ("/* Generated automatically by the program `genemit'\n\
  438. from the machine description file `md'.  */\n\n");
  439.  
  440.   printf ("#include \"config.h\"\n");
  441.   printf ("#include \"rtl.h\"\n");
  442.   printf ("#include \"expr.h\"\n");
  443.   printf ("#include \"real.h\"\n");
  444.   printf ("#include \"insn-config.h\"\n\n");
  445.   printf ("#include \"insn-flags.h\"\n\n");
  446.   printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
  447.   printf ("extern rtx recog_operand[];\n");
  448.   printf ("#define operands emit_operand\n\n");
  449.   printf ("#define FAIL do { end_sequence (); return 0;} while (0)\n\n");
  450.   printf ("#define DONE goto _done\n\n");
  451.  
  452.   /* Read the machine description.  */
  453.  
  454.   while (1)
  455.     {
  456.       c = read_skip_spaces (infile);
  457.       if (c == EOF)
  458.     break;
  459.       ungetc (c, infile);
  460.  
  461.       desc = read_rtx (infile);
  462.       if (GET_CODE (desc) == DEFINE_INSN)
  463.     {
  464.       gen_insn (desc);
  465.       ++insn_code_number;
  466.     }
  467.       if (GET_CODE (desc) == DEFINE_EXPAND)
  468.     {
  469.       gen_expand (desc);
  470.       ++insn_code_number;
  471.     }
  472.       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  473.     {
  474.       ++insn_code_number;
  475.     }
  476.     }
  477.  
  478.   fflush (stdout);
  479.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  480. }
  481.